package com.thon.controller.rest;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.user.Letter;
import com.thon.entity.user.LetterView;
import com.thon.service.user.LetterService;

@Controller
@RequestMapping(value = "/api/letter")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class LetterRestController {

	@Autowired
	private LetterService letterService;

	// 根据用户双方id获取date时间点之前的对话列表历史
	@RequestMapping(value = "/list/old")
	public ResponseEntity<HttpEntity> listOldLetters(
			@RequestParam(value = "uid", required = true) Integer userId,
			@RequestParam(value = "fid", required = true) Integer friendId,		
			@RequestParam(value = "p", defaultValue = "1") int pageNo,
			@RequestParam(value = "s", defaultValue = "10") int pageSize,
			Model model) {

		Date date = new Date();
		Page<LetterView> page = new Page<LetterView>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = letterService.listOldLetters(page, userId, friendId,date);
		for (LetterView letterView : page.getResult()) {
			letterView.setIsRead(1);
			letterService.updateLetter(letterView);
		}
		return new ResponseEntity(page, HttpStatus.OK);
	}
	
	// 根据用户双方id获取date时间点之后的对话列表
	@RequestMapping(value = "/list/new")
	public ResponseEntity<HttpEntity> listLetters(
			@RequestParam(value = "uid", required = true) Integer userId,
			@RequestParam(value = "fid", required = true) Integer friendId,			
			@RequestParam(value = "p", defaultValue = "1") int pageNo,
			@RequestParam(value = "s", defaultValue = "10") int pageSize,
			Model model) {

		Date date = new Date();
		Page<LetterView> page = new Page<LetterView>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = letterService.listNewLetters(page, userId, friendId,date);
		
		for (LetterView letterView : page.getResult()) {
			letterView.setIsRead(1);
			letterService.updateLetter(letterView);
		}
		return new ResponseEntity(page, HttpStatus.OK);
	}

	// 根据用户id获取联系人列表
	@RequestMapping(value = "/contacter")
	public ResponseEntity<HttpEntity> listNewestLetters(
			@RequestParam(value="uid") Integer userId, 
			@RequestParam(value="p",defaultValue="1") Integer pageNo,
			@RequestParam(value="s",defaultValue="3") Integer pageSize,
			Model model) {

		Page<LetterView> letterViews = letterService.listNewestLetters(userId,pageNo,pageSize);
		model.addAttribute("newestLetter", letterViews);
		return new ResponseEntity(model, HttpStatus.OK);
	}

	// 发私信
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> sendLetter(
			@Valid @RequestBody Letter letter) {

		Integer userId = letter.getUserId();
		Integer friendId = letter.getFriendId();
		String content = letter.getContent();

		Date createdDate = new Date();
		Letter userLetter = new Letter();

		userLetter.setContent(content);
		userLetter.setCreatedDate(createdDate);
		userLetter.setIsRead(1);

		userLetter.setLetterFrom(userId);
		userLetter.setLetterTo(friendId);
		userLetter.setUserId(userId);
		userLetter.setFriendId(friendId);

		letterService.addLetter(userLetter);

		Letter friendLetter = new Letter();
		friendLetter.setContent(content);
		friendLetter.setCreatedDate(createdDate);
		friendLetter.setIsRead(0);
		friendLetter.setLetterFrom(userId);
		friendLetter.setLetterTo(friendId);
		friendLetter.setUserId(friendId);
		friendLetter.setFriendId(userId);

		letterService.addLetter(friendLetter);
		return new ResponseEntity(userLetter, HttpStatus.OK);
	}

}
